<?php
/**
 * @copyright (C) veapon.com 2012
 * @file admin.class.php
 * @brief 管理员操作类
 * @author veapon 
 * @version v1.0
 * @date 2012-03-19
 */

class adminModel extends Model{
	private $tbAdmin = 'admin';

	public function __construct(){
		parent::__construct($this->tbAdmin);	
	}

	public function get_admin_by_id($id){
	
	}

	/**
	 * @brief get_admin_by_name 根据账号获取管理员信息
	 * @param string $name 账号
	 * @return array OR false
	 * @version v1.0
	 * @date 2012-03-19
	 */
	public function get_admin_by_name($name){
		if( !empty($name) ){
			return $this->where("name='".htmlspecialchars($name)."'")->get_one();
		}	
	}

	/**
	 * @brief add_admin 新增管理员
	 * @param array $data 管理员信息
	 * @return int OR boolean
	 * @version v1.0
	 * @date 2012-03-19
	 */
	public function add_admin($data){
		if( empty($data) || !is_array($data) ){
			$this->error( lang('invalid_data_type') );
		}
		foreach( $data as $k=>$v ){
			if( ($k=='name' || $k=='password') && empty($v) ){
				$this->error( lang('item_required') );
			}else{
				$v = trim($v);
				if( !empty($v) ) $data[$k] = htmlspecialchars($v);
			}
		}
		if( strlen($data['password'])<5 || strlen($data['password'])>15 ){
			$this->error( lang('passwd_len_error') );
		}
		if( $data['password']!==$data['password2'] ){
			$this->error( lang('passwd_mismatch') );
		}
		$data['password'] = md5($data['password']);
		$data['nicename'] = $data['name'];
		$data['is_banned'] = 0;
		if( $this->get_admin_by_name($data['name']) ){
			$this->error( lang('user_exists') );
		}
		return $this->value($data)->add($data);
	}

	public function check_login($name, $pwd){
		if( empty($name) || empty($pwd) ){
			$this->error( lang('name_pwd_null') );
		}
		$data= array('name'=>$name, 'password'=>$pwd);
		$rs = $this->where($data)->get_one();
		if( is_array($rs) && count($rs)>0 ){
			return $rs;	
		}else{
			$this->error( lang('name_pwd_error') );
		}
	}
}
